﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
//using System.Web.Mvc;

namespace Common.Tools
{
    public class EnumHelper
    {
        ///// <summary>
        ///// 将指定枚举类型转换成List，用来绑定ListControl
        ///// </summary>
        ///// <param name="enumType">枚举类型</param>
        ///// <param name="selectFeild">默认选择的项的值</param>
        ///// <returns></returns>
        //public static IEnumerable<SelectListItem> ConvertEnumToListItems(Type enumType, string selectFeild)
        //{
        //    if (enumType.IsEnum == false) { yield return null; }
        //    var typeDescription = typeof(DescriptionAttribute);
        //    var fields = enumType.GetFields();
        //    foreach (var field in fields.Where(field => !field.IsSpecialName))
        //    {
        //        var strValue = field.GetRawConstantValue().ToString();
        //        var arr = field.GetCustomAttributes(typeDescription, true);
        //        var strText = arr.Length > 0 ? ((DescriptionAttribute)arr[0]).Description : field.Name;

        //        var item = new SelectListItem { Text = strText, Value = strValue };
        //       ListItem
        //        if (strValue.Equals(selectFeild))
        //            item.Selected = true;
        //        yield return item;
        //    }
        //}

        ///// <summary>
        ///// 将指定枚举类型转换成List，用来绑定ListControl
        ///// </summary>
        ///// <param name="enumType">枚举类型</param>
        ///// <param name="selectFeild">默认选择的项的值</param>
        ///// <returns></returns>
        //public static List<SelectListItem> ConvertEnumToList(Type enumType, string selectFeild)
        //{
        //    if (enumType.IsEnum == false) { return null; }
        //    var typeDescription = typeof(DescriptionAttribute);
        //var fields = enumType.GetFields();
        //var selectListItems = new List<SelectListItem>();
        //    foreach (var field in fields.Where(field => !field.IsSpecialName))
        //    {
        //        var strValue = field.GetRawConstantValue().ToString();
        //var arr = field.GetCustomAttributes(typeDescription, true);
        //var strText = arr.Length > 0 ? ((DescriptionAttribute)arr[0]).Description : field.Name;

        //var item = new SelectListItem { Text = strText, Value = strValue };

        //        if (strValue.Equals(selectFeild))
        //            item.Selected = true;
        //        selectListItems.Add(item);
        //    }
        //    return selectListItems;
        //}
    }
}
